
import java.awt.Color;
import java.sql.*;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/*
 * Login.java
 *
 * Created on 12-05-2010, 11:42:16
 */
/**
 * @author binh
 */
class Account{
    String userName;
    String password;
}
public class AccountManagement extends javax.swing.JFrame {
    // Variable to store a reference to a Connection object.
    private java.sql.Connection conn = null;
    // Variable to store the URL connection string following type driver 4
    private final String url = "jdbc:sqlserver://";
    // Variable to store the IP address of the machine on which SQL Server 2005 database is located.
    private final String serverName = "BINH-PC";
    // Variable to store the default port number of SQL Server Database.
    private final String portNumber = "1433";
    // Variable to store the instance name of the SQL Server.
    // private final String instanceName = "martin";
    // Variable to store the name of the database to connect to.
    private final String databaseName = "Account";
    // Variable to store the user name to connect to SQL Server.
    private String userName = "sa";
    // Variable to store the pasword to connect to SQL Server.
    private String password = "";
    //String variable to store the parameterized query string.
    private String sqlStr = "";
    // Statement object to hold a reference to the PreparedStatement object.
    private PreparedStatement pstmt = null;
    // ResultSet object to hold a reference to the ResultSet object.
    private ResultSet rs = null;

    /** Creates new form Login */
    public AccountManagement() {
        try {
          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
          UIManager.put("Button.showMnemonics", Boolean.TRUE);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
        getContentPane().setBackground(new Color(212, 208, 200));
        // Load Driver for connecting to SQL Server 2008 database.
        try {
              //1a- Obtain a connection to the database - Driver 4
                 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                 conn = DriverManager.getConnection(this.getConnectionUrl(), userName, password);
              //1b- Obtain a connection to the database - Driver 1 with Data source: Demo
                //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Step 1
                //conn = DriverManager.getConnection("jdbc:odbc:Demo", userName, password); // Step 2
                if(conn != null) {
                    JOptionPane.showMessageDialog(null, "Connection is Successful!!");
                }
        } catch (SQLException ex) {
            if(conn == null) {
                JOptionPane.showMessageDialog(null,"Connection is not successful!");
            }
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Class does not found!");
            ex.printStackTrace();
        }
        initComponents();
        this.fillData(); // Fill data into cbUser
        this.setBounds(400, 100, 520, 400);
    }
    private String getConnectionUrl() {
        //return url + serverName + ";instanceName=" + instanceName + ";databaseName="+ databaseName;
        return url + serverName + ":" + portNumber + "; databaseName=" + databaseName;
    }
    private void closeConnection(){
        try{
            if(conn != null)
            {
                conn.close();
            }
            conn = null;
            pstmt = null;
            rs = null;
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    /** This method is called from within the constructor to Initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtUserName = new javax.swing.JTextField();
        btnLogin = new javax.swing.JButton();
        btnInsert = new javax.swing.JButton();
        btnView = new javax.swing.JButton();
        scpLogin = new javax.swing.JScrollPane();
        tbLogin = new javax.swing.JTable();
        btnClear = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnFirst = new javax.swing.JButton();
        btnPrevious = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        btnLast = new javax.swing.JButton();
        txtPassword = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        cbUser = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Account Management");

        jLabel1.setText("User Name");

        jLabel2.setText("Password");

        btnLogin.setText("LOGIN");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        btnInsert.setText("INSERT");
        btnInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInsertActionPerformed(evt);
            }
        });

        btnView.setText("VIEW");
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });

        tbLogin.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "UserName", "Password"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbLogin.setColumnSelectionAllowed(true);
        tbLogin.getTableHeader().setReorderingAllowed(false);
        tbLogin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbLoginMouseClicked(evt);
            }
        });
        scpLogin.setViewportView(tbLogin);
        tbLogin.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tbLogin.getColumnModel().getColumn(1).setResizable(false);

        btnClear.setText("CLEAR");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        btnClose.setText("CLOSE");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        btnUpdate.setText("UPDATE");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnDelete.setText("DELETE");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnFirst.setText("FIRST");
        btnFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFirstActionPerformed(evt);
            }
        });

        btnPrevious.setText("PREVIOUS");
        btnPrevious.setEnabled(false);
        btnPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreviousActionPerformed(evt);
            }
        });

        btnNext.setText("NEXT");
        btnNext.setEnabled(false);
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        btnLast.setText("LAST");
        btnLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLastActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel3.setText("ACCOUNT MANAGEMENT");

        jLabel4.setText("List of User Name");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(btnLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(13, 13, 13)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                    .addComponent(jLabel1)
                                                    .addComponent(jLabel4)))
                                            .addComponent(btnFirst, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
                                        .addGap(19, 19, 19))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(18, 18, 18)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(btnPrevious, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                                            .addComponent(btnInsert, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                                            .addComponent(cbUser, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(btnNext, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(btnView, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(txtUserName, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txtPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(64, 64, 64)
                                .addComponent(jLabel3)))
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnLast, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnClose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(scpLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnDelete)
                        .addGap(18, 18, 18)
                        .addComponent(btnUpdate)
                        .addGap(18, 18, 18)
                        .addComponent(btnClose)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnView)
                    .addComponent(btnInsert)
                    .addComponent(btnLogin)
                    .addComponent(btnClear))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNext)
                    .addComponent(btnPrevious)
                    .addComponent(btnFirst)
                    .addComponent(btnLast))
                .addGap(18, 18, 18)
                .addComponent(scpLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-509)/2, (screenSize.height-422)/2, 509, 422);
    }// </editor-fold>//GEN-END:initComponents
    private void fillData()
    {
        pstmt = null;
        rs = null;
        //Step 1- String variable to store the parameterized query string.
        sqlStr = "SELECT userName FROM Login";
        try {
            // Step 2- PreparedStatement object for sending SQL statement to the database.
            pstmt = conn.prepareStatement(sqlStr);
            // Step 3- Create object ResultSet rs
            rs = pstmt.executeQuery();
            // Step 4- Fill cbUser from rs
            while( rs.next()){
                cbUser.addItem(rs.getString(1));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    //
    private void displayData(){
        pstmt = null;
        rs = null;
        Vector  accountData = new Vector ();
        Vector <String> columnNames = new Vector <String>();

        columnNames.add("User Name");
        columnNames.add("Password");
        //String variable to store the parameterized query string.
        sqlStr = "Select * from Login";
        Vector<String> row = null;
        try {
            // PreparedStatement object for sending SQL statement to the database.
            pstmt = conn.prepareStatement(sqlStr);
            rs = pstmt.executeQuery();
            while(rs.next()){
                row = new Vector<String>();
                row.addElement(rs.getString("userName")); // userName is column name of table Login
                row.addElement(rs.getString("password")); // password is column name of table Login
                accountData.add(row);
            }
            tbLogin.setModel(new DefaultTableModel(accountData, columnNames));
            scpLogin.setViewportView(tbLogin);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        pstmt = null;
        rs = null;
        String name = this.txtUserName.getText();
        String pass = this.txtPassword.getText();
        if(name.trim().equals(""))
        {
            JOptionPane.showMessageDialog(null,"User name is not blank");
            this.txtUserName.requestFocus();
            return;
        }
        if(pass.trim().equals(""))
        {
            JOptionPane.showMessageDialog(null,"Password is not blank");
            this.txtPassword.requestFocus();
            return;
        }
        //Step 1- String variable to store the parameterized query string.
        sqlStr = "SELECT * FROM Login where userName = '" + name
                  + "' and password = '" + pass + "'";
        try {
            // Step 2- PreparedStatement object for sending SQL statement to the database.
            pstmt = conn.prepareStatement(sqlStr);
            //Statement st = conn.createStatement();
            // Step 3- Create object ResultSet rs
            rs = pstmt.executeQuery();
            //rs = st.executeQuery(sqlStr);
            // Step 4- Checking object rs
            if(rs.next()){
                JOptionPane.showMessageDialog(null, "Login is successful! Welcome " + name);
            }
            else{
                JOptionPane.showMessageDialog(null, "Login failed. Username or password is invalid. Please again!");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnLoginActionPerformed

    private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertActionPerformed
        pstmt = null;
        rs = null;
        String name = this.txtUserName.getText();
        String pass = this.txtPassword.getText();

        if(name.trim().equals(""))
        {
            JOptionPane.showMessageDialog(null,"User name is not blank");
            this.txtUserName.requestFocus();
            return;
        }
        if(pass.trim().equals(""))
        {
            JOptionPane.showMessageDialog(null,"Password is not blank");
            this.txtPassword.requestFocus();
            return;
        }
        //String variable to store the parameterized query string.
        sqlStr = "Select * from Login where userName = '" + name + "'";
        try {
            // PreparedStatement object for sending SQL statement to the database.
            pstmt = conn.prepareStatement(sqlStr);
            rs = pstmt.executeQuery();
            if(rs.next()){ // If existed username
                JOptionPane.showMessageDialog(null, "User name " + name + " existed. Please accept other user name!");
                this.txtUserName.requestFocus();
                this.txtUserName.selectAll();
            }
            else{
                sqlStr = "Insert into Login values('" + name + "','" + pass + "')";
                pstmt = conn.prepareStatement(sqlStr);
                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(null, "The record is inserted");
                this.displayData();
                this.cbUser.removeAllItems();
                this.fillData();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnInsertActionPerformed
    
    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
        // TODO add your handling code here:
        pstmt = null;
        rs = null;       
        this.displayData(); // Refresh data in the table
    }//GEN-LAST:event_btnViewActionPerformed

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        // TODO add your handling code here:
        this.txtUserName.setText("");
        this.txtPassword.setText("");
    }//GEN-LAST:event_btnClearActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        this.closeConnection();
        try {
            // TODO add your handling code here:           
            this.dispose();
        } catch (Exception ex) {
            Logger.getLogger(AccountManagement.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_btnCloseActionPerformed

    private void tbLoginMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbLoginMouseClicked
        // TODO add your handling code here:
        String username = (String)tbLogin.getValueAt(tbLogin.getSelectedRow(), 0);
        String password = (String)tbLogin.getValueAt(tbLogin.getSelectedRow(), 1);
        this.txtUserName.setText(username);
        this.txtPassword.setText(password);
    }//GEN-LAST:event_tbLoginMouseClicked

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        pstmt = null;
        rs = null;
        String name = this.txtUserName.getText();

        if(name.trim().equals(""))
        {
            JOptionPane.showMessageDialog(null,"User name is not blank");
            this.txtUserName.requestFocus();
            return;
        }
        //String variable to store the parameterized query string.
        sqlStr = "Select * from Login where userName = '" + name + "'";
        try {
            // PreparedStatement object for sending SQL statement to the database.
            pstmt = conn.prepareStatement(sqlStr);
            rs = pstmt.executeQuery();
            if(!rs.next()){
                JOptionPane.showMessageDialog(null, "User name " + name + " is not exist. Please accept other user name!");
                this.txtUserName.selectAll();
            }
            else{
                if(JOptionPane.showConfirmDialog(this, "Are you sure to delete user name: " + name, "Confirm deleting ...", JOptionPane.YES_NO_CANCEL_OPTION) == JOptionPane.YES_OPTION)
                {
                    sqlStr = "Delete From Login Where username ='" + name + "'";
                    pstmt = conn.prepareStatement(sqlStr);
                    pstmt.executeUpdate();
                    this.displayData(); // Refresh data in the table
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFirstActionPerformed
        // TODO add your handling code here:
        pstmt = null;
        rs = null;
        // PreparedStatement object for sending the SQL statement to the database.
        sqlStr = "Select * from Login";
        try{
            pstmt = conn.prepareStatement(sqlStr, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            // Execute SQL statement in PreparedStatement object and return
            // the ResultSet object generated by the query.
            rs = pstmt.executeQuery();
            rs.first();
            btnNext.setEnabled(true);
            btnPrevious.setEnabled(false);
            txtUserName.setText(rs.getString(1));
            txtPassword.setText(rs.getString(2));
        }catch(SQLException e){
            e.printStackTrace();
        }

}//GEN-LAST:event_btnFirstActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        // TODO add your handling code here:
        try {
          rs.next();
          btnFirst.setEnabled(true);
          btnPrevious.setEnabled(true);
          if(rs.isAfterLast() || rs.isBeforeFirst()){
            btnNext.setEnabled(false);
            JOptionPane.showMessageDialog(null, "You have reached the last record of the ResultSet!!!!");
          } else {
            txtUserName.setText(rs.getString(1));
            txtPassword.setText(rs.getString(2));
          }
        } catch (SQLException ex) {
          ex.printStackTrace();
        }
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPreviousActionPerformed
        // TODO add your handling code here:
        try {
          rs.previous();
          btnFirst.setEnabled(true);
          btnNext.setEnabled(true);
          if(rs.isBeforeFirst()){
            btnPrevious.setEnabled(false);
            btnNext.setEnabled(true);
            JOptionPane.showMessageDialog(null, "You have reached the first record of the ResultSet!!!!");
          } else {
            btnPrevious.setEnabled(true);
            txtUserName.setText(rs.getString(1));
            txtPassword.setText(rs.getString(2));
          }
        } catch (SQLException ex) {
          ex.printStackTrace();
        }
    }//GEN-LAST:event_btnPreviousActionPerformed

    private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLastActionPerformed
        // TODO add your handling code here:
        try {
          rs.last();
          btnFirst.setEnabled(true);
          btnNext.setEnabled(false);
          btnPrevious.setEnabled(true);
          txtUserName.setText(rs.getString(1));
          txtPassword.setText(rs.getString(2));
        } catch (SQLException ex) {
          ex.printStackTrace();
        }
    }//GEN-LAST:event_btnLastActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:
        pstmt = null;
        rs = null;
        String name = this.txtUserName.getText();
        String pass = this.txtPassword.getText();

        if(name.trim().equals(""))
        {
            JOptionPane.showMessageDialog(null,"User name is not blank");
            this.txtUserName.requestFocus();
            return;
        }
        if(pass.trim().equals(""))
        {
            JOptionPane.showMessageDialog(null,"Password is not blank");
            this.txtPassword.requestFocus();
            return;
        }

        //String variable to store the parameterized query string.
        sqlStr = "Select * from Login where userName = '" + name + "'";
        try {
            // PreparedStatement object for sending SQL statement to the database.
            pstmt = conn.prepareStatement(sqlStr);
            rs = pstmt.executeQuery();
            if( !rs.next()){
                JOptionPane.showMessageDialog(null, "User name " + name + " is not exist. Please accept other user name!");
                this.txtUserName.selectAll();
            }
            else{
                sqlStr = "Update Login Set password ='" + pass + "' where username ='" + name + "'";
                pstmt = conn.prepareStatement(sqlStr);
                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(null, "The record is updated");
                this.displayData(); // Refresh data in the table
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnUpdateActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AccountManagement().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnFirst;
    private javax.swing.JButton btnInsert;
    private javax.swing.JButton btnLast;
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrevious;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JButton btnView;
    private javax.swing.JComboBox cbUser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JScrollPane scpLogin;
    private javax.swing.JTable tbLogin;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables

}
